package beyjava.view;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.Vector;

import beyjava.model.Project;
import beyjava.util.IdGenerator;
import beyjava.util.Validator;
import beyjava.util.Common;

/**
 * @author BEYJAVA
 * project view class
 */
public class ProjectMenu {
	Scanner sc;
     /**
     * constructor of projectMenu class 
     */
    public ProjectMenu(){
    	sc = new Scanner(System.in);
    }
    /**
     * @return choose of the user for project operations
     * method to list project operation and get choose from user
     */
    public int chooseMainMenu(){// maindeki show menu;
  		System.out.println("Please Choose A System Job");
  		System.out.println("1-Add Project");
  		System.out.println("2-Remove Project");
  		System.out.println("3-Update Project");
  		System.out.println("4-Report to CSV");
  		System.out.println("5-Export to Excel");
  		System.out.println("6-Report All System to Console");
  		return Common.getChoose(1, 6);
  	}
    /**
     * @return addedProject
     * @throws IllegalArgumentException
     * method to get properties of project which is wanted to add from user
     */
    public Project addProjectMenu(){
	     Project addedProject = null;
	     Integer projectID;
		 String projectName;
		 String projectDesc;
		 Date projectStartDate;
		 try{
			 System.out.println("Please Fill the Required Field For Adding a Project.");
			 System.out.println("Project Name :");
			 projectName = sc.next();
			 Validator.textHasContent(projectName);
			 System.out.println("Project Description :");
			 projectDesc = sc.next();
			 Validator.textHasContent(projectDesc);
			 projectID = IdGenerator.generateIntId();
			 System.out.println("Project Start Date(Date format must be in this format dd-MM-yyyy) :");
			 projectStartDate  = Common.getDate("dd-MM-yyyy");
			 addedProject = new Project(projectID,projectName, projectDesc,projectStartDate);
		 }
		 catch(IllegalArgumentException ex){
 			 System.out.println(ex.getMessage());
		 }
		 return addedProject;
	}
    /**
     * @param projects
     * @return project which is chosen from user to remove operation
     * method to list available projects and get choose of user
     */
    public  Project removeProjectMenu(Vector<Project> projects){
  		if(projects == null || projects.size() < 1){
  			System.out.println("There is no project in system. Please add a pjocet");
  			return null;
  		}
  	    return selectProjectMenu(projects,"remove");
  	}     
    /**
     * @param projects
     * @return project which is chosen from user to update operation
     * method to list available projects and get choose of user
     */
    public Project updateProjectMenu(Vector<Project> projects){
    	if(projects == null || projects.size() < 1){
  			System.out.println("There is no project in system. Please add a pjocet");
  			return null;
  		}
    	return selectProjectMenu(projects,"update");
    }
	/**
	 * @param updatedProject
	 * @return choose of user to get which property of project is updated.
	 * method to list property of project which is wanted to update and 
	 * get property choose to update from user
	 */
	public int selectProjectUpdateAction(Project updatedProject){
    	String DATE_FORMAT = "dd-MM-yyyy";
    	SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    	System.out.println("Please choose one of to project's information to update.");
		System.out.println("1- Project Name : " + updatedProject.getProjectName());
		System.out.println("2- Project Description : " + updatedProject.getProjectDesc());
		System.out.println("3- Project StartDate : " + sdf.format(updatedProject.getProjectStartDate()));
		System.out.println("4- Please enter 4 for activity operations." );
		System.out.println("0- Return Previous Menu");
		return Common.getChoose(0, 4);
    } 
    /**
     * @param updatedProject
     * @param action
     * @return newProject which has updated properties of updatedProject
     * method to get new properties of updated project from user
     */
    public Project updateProject(Project updatedProject , int action){
    	String y;
    	Project newProject = new Project(updatedProject.getProjectID(),updatedProject.getProjectName(),updatedProject.getProjectDesc(),updatedProject.getProjectStartDate());
    	switch(action){
		case 1: System.out.println("Please enter new Project Name :");
			y = sc.next();
			Validator.textHasContent(y);
			newProject.setProjectName(y);
			break;
		case 2: System.out.println("Please enter new Project Description :");
			y = sc.next();
			Validator.textHasContent(y);
			newProject.setProjectDesc(y);
			break;
		case 3: System.out.println("Please enter new Project StartDate :");
			Date date = Common.getDate("dd-MM-yyyy");
			newProject.setProjectStartDate(date);
			break;
    	}
    	return newProject;
    }
     /**
     * @param projects
     * @param action
     * @return project which is chosen from user to do wanted operation
     */
    public Project selectProjectMenu(Vector<Project> projects,String action){
   	    System.out.println("Your projects:");
  		for(int i = 0;i < projects.size();i++){
  			System.out.println((i+1)+"-"+projects.get(i).getProjectName().toUpperCase());
  		}
  		System.out.println("Please Write One Of The Project's Number To "+action+":");
  		int choose = Common.getChoose(1, projects.size());
  		return projects.get(choose-1);
      }
}  
